/**
 * @Author Mie Plougstrup, Anders Abildgaard, Bo Stokholm
 * @Version 09.11.2014
 * 
 * This class is used to create the connection to the database.
 * It is implemented as a singleton class.
 */

package dbLayer;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;

public class DbConnection {

	//constants used to get access to the database.
	//SQL Server.
	private static final String  driver = "jdbc:sqlserver://balder.ucn.dk:1433";
	private static final String  databaseName = ";databaseName=dmaa0214Projekt_1";

	private static String  userName = "; user=dmaa0214Projekt_1";
	private static String password = ";password=Biksemad";

	private DatabaseMetaData dma;
	private static Connection con;
	// an instance of the class is generated.
	private static DbConnection  instance;

	/**
	 * The constructor is private to ensure that only one instance of this class is created.
	 * 
	 * @throws NullPointerException
	 */
	private DbConnection() throws NullPointerException {
		String url = driver + databaseName + userName + password;

		try {
			//load of driver
			//SQL Server
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			System.out.println("Load af class ok");

		} catch(Exception e) {

			System.out.println("Can not find the driver");
			System.out.println(e.getMessage());
		}

		try{
			//connection to the database
			con = DriverManager.getConnection(url);
			//set auto-commit
			con.setAutoCommit(true);
			dma = con.getMetaData(); // get meta data
			System.out.println("Connection to " + dma.getURL());
			System.out.println("Driver " + dma.getDriverName());
			System.out.println("Database product name " + dma.getDatabaseProductName());

		} catch(Exception e) {

			System.out.println("Problems with the connection to the database");
			System.out.println(e.getMessage());
			System.out.println(url);
			throw new NullPointerException("Error DBConnection");
		}
	}//end  constructor

	/**
	 * Method for getting the instance of the connection.
	 * 
	 * @return The instance of this class.
	 */
	public static DbConnection getInstance() {
		if (instance == null) {
			try {
				instance = new DbConnection();
			} catch(NullPointerException e)	{ 
				System.out.println(e.getMessage());
				instance = null;
			}
		}
		return instance;
	}

	/**
	 * Closes the connection to the database.
	 */
	public static void closeConnection() {
		try {
			con.close();
			System.out.println("The connection is closed");

		} catch (Exception e) {

			System.out.println("Error trying to close the database " +  e.getMessage());
		}
	}

	/**
	 * Start transaction
	 */
	public static void startTransaction() {
		try {
			con.setAutoCommit(false);

		} catch(Exception e) {

			System.out.println("Error in starting the transaction");
			System.out.println(e.getMessage());
		}
	}

	/**
	 * Commit transaction
	 */
	public static void commitTransaction() {
		try {
			con.setAutoCommit(true);

		} catch(Exception e) {

			System.out.println("Error in commiting the transaction");
			System.out.println(e.getMessage());
		}
	}

	/**
	 * Roll back transaction
	 */
	public static void rollbackTransaction() {
		try {
			con.rollback();
			con.setAutoCommit(true);

		} catch(Exception e) {
			System.out.println("Error in rollback of transaction");
			System.out.println(e.getMessage());
		}
	}

	/**
	 * Accessor-method
	 * 
	 * @return the connection.
	 */
	public  Connection getDBcon() {
		return con;
	}
}